package org.example.service01.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.example.service01.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

@RestController
@RefreshScope
public class TestController {

    //读取当前应用的启动端口
    @Value("${server.port}")
    private String applicationServerPort;

    /*@Autowired
    private DataSource dataSource;

    @Value("${user.id:0}")
    private String userId;
    @Value("${user.name1:zhangsan}")
    private String userName;

    @GetMapping("/hello")
    public String hello() {
        return "hello service01";
    }*/

    @GetMapping("/service01/test01")
    @SentinelResource(value = "test01")
    public String test01() {
        //返回信息给调用段
        return "hello I am service01，" + applicationServerPort;
    }

    /*@GetMapping("/test02")
    public String test02() throws SQLException {
        String dataSourceClass = dataSource.getClass().toString();
        Boolean haveConnection = dataSource.getConnection() == null ? true : false;

        return "数据源类型为：" + dataSourceClass + "，" + "数据库连接是否成功：" + haveConnection;
    }
*/
    @Value("${user.info:0}")
    private String configInfo;

    @GetMapping("/test03")
    public String test03() throws SQLException {
        return "configInfo：" + configInfo;
    }

    @Value("${mysql.info:0}")
    private String mysqlInfo;
    @Value("${redis.info:0}")
    private String redisInfo;

    @GetMapping("/test04")
    public String test04() throws SQLException {
        return "configInfo：" + configInfo + "，mysqlInfo：" + mysqlInfo + "，redisInfo：" + redisInfo;
    }


    @GetMapping("/test05")
    public String test05(@RequestParam("name") String name, @RequestParam("ids") Integer[] ids, @RequestParam("habbits") List<String> habbits) throws SQLException {
        return "hello " + name + "，ids：" + Arrays.toString(ids) + "，habbits：" + Arrays.toString(StringUtils.toStringArray(habbits));
    }

    @PostMapping("/test06")
    public String test06(@RequestBody User user) throws SQLException {
        return user.toString();
    }

    @GetMapping("/test07/{name}")
    public String test07(@PathVariable String name) throws SQLException {
        return "hello service01 :" +  name + " - " + applicationServerPort;
    }

    @GetMapping("/service01/test08")
    public String test08() throws SQLException {
        return "hello service01 test08";
    }

    @GetMapping("/service01/test09")
    public String test09() throws SQLException {
        return "hello service01 test09";
    }

    @GetMapping("/test10")
    @SentinelResource("getAgeResource") // 显式定义资源名
    public Integer test10() throws SQLException {
        Integer age = test12();
        return age;
    }

    @GetMapping("/test11")
    @SentinelResource("getAgeResource") // 显式定义资源名
    public Integer test11() throws SQLException {
        Integer age = test12();
        return age;
    }


    public Integer test12() {
        return new User().getAge();
    }
}
